/**
 * create by Jane
 * 网站基础配置信息
 */
import {db, app, setTcbCloudImages} from "../config/database";
import Utils from "../utils";
import config from "../../cloudbaserc.json";

/**
 * 查询一条数据
 * @param collection
 * @param where
 * @returns {Promise<null|*>}
 */
const queryOne = async (collection, where) => {
    if (collection && where) {
        const res = await db.collection(collection).where(where).get();
        if (res && res.data) {
            return res.data[0];
        }
    }
    return null;
}

/**
 * 查询网站的基础信息
 */
export const getBaseData = async () => {
    const res = await db.collection("hc-baseInfo").get();
    if (res && res.data) {
        return await setTcbCloudImages(res.data);
    }
    return [];
}

/**
 * 查询兄弟公司基本信息
 * @returns {Promise<void>}
 */
export const getCompanyData = async () => {
    const res = await db.collection("hc-companys").get();
    if (res && res.data) {
        return await setTcbCloudImages(res.data, 'logo');
    }
    return [];
}

/**
 * 查询首页轮播数据列表
 */
export const getCarouselList = async () => {
    const res = await db.collection("hc-carousel").get();
    if (res && res.data) {
        return await setTcbCloudImages(res.data);
    }
    return [];
}

/**
 * 查询【公司资讯文章内容】
 */
export const getInformationList = async (data = {}) => {
    const {paging = null} = data;
    let where = {
        status: true
    };
    let col = db.collection("hc-news").where(where);
    // 分页查询数据
    if (!Utils.isNull(paging)) {
        col = col
            .skip((paging.currPage - 1) * paging.pageSize)
            .limit(paging.pageSize)
    }
    const res = await col
        .orderBy("_createTime", "desc")
        .get();
    if (res && res.data) {
        return await setTcbCloudImages(res.data);
    }
    return [];
}

/**
 * 更新【公司资讯】信息
 * @returns {Promise<*[]|void>}
 */
export const editInformation = async ({id}) => {
    let info = await queryOne("hc-news", {_id: id});
    if (!info) {
        return alert('数据错误！')
    }
    let res = await app.callFunction({
        name: "vue-echo",
        data: {action: 'editInformation', data: {id}}
    });
    return res;
}

/**
 * 查询【项目案例】数据
 * @param data 数据查询条件
 * @returns {Promise<*[]|void>}
 */
export const getProjectList = async (data = {}) => {
    const {paging = null} = data;
    let where = {
        status: true
    };
    let col = db.collection("hc-projects").where(where);
    // 分页查询数据
    if (!Utils.isNull(paging)) {
        col = col
            .skip((paging.currPage - 1) * paging.pageSize)
            .limit(paging.pageSize)
    }
    const res = await col
        .orderBy("_createTime", "desc")
        .get();
    if (res && res.data) {
        return await setTcbCloudImages(res.data);
    }
    return [];
}
/**
 * 更新【项目案例】信息
 * @returns {Promise<*[]|void>}
 */
export const editProject = async ({id}) => {
    let info = await queryOne("hc-projects", {_id: id});
    if (!info) {
        return alert('数据错误！')
    }
    let res = await app.callFunction({
        name: "vue-echo",
        data: {action: 'editProject', data: {id}}
    });
    return res;
}
/**
 * 查询【服务内容】数据
 * @param data 数据查询条件
 * @returns {Promise<*[]|void>}
 */
export const getServicesList = async (data = {}) => {
    const {paging = null} = data;
    let where = {
        status: true
    };
    let col = db.collection("hc-services").where(where);
    // 分页查询数据
    if (!Utils.isNull(paging)) {
        col = col
            .skip((paging.currPage - 1) * paging.pageSize)
            .limit(paging.pageSize)
    }
    const res = await col
        .orderBy("_createTime", "desc")
        .get();
    if (res && res.data) {
        return await setTcbCloudImages(res.data);
    }
    return [];
}
export const editService = async ({id}) => {
    let info = await queryOne("hc-services", {_id: id});
    if (!info) {
        return alert('数据错误！')
    }
    let res = await app.callFunction({
        name: "vue-echo",
        data: {action: 'editService', data: {id}}
    });
    return res;
}


/**
 * 查询【企业团队】数据
 */
export const getTeamList = async (data = {}) => {
    const {paging = null} = data;
    let col = db.collection("hc-team")
    // 分页查询数据
    if (!Utils.isNull(paging)) {
        col = col
            .skip((paging.currPage - 1) * paging.pageSize)
            .limit(paging.pageSize)
    }
    const res = await col.get();
    if (res && res.data) {
        return await setTcbCloudImages(res.data, 'avatar');
    }
    return [];
}


/**
 * 查询【技术优势】数据
 */
export const getTechnologicalData = async () => {
    const res = await db.collection("hc-technological").get();
    if (res && res.data) {
        return await setTcbCloudImages(res.data);
    }
    return [];
}

/**
 * 查询【服务项目】数据
 */
export const getServiceProjectData = async () => {
    const res = await db.collection("hc-service-project").get();
    if (res && res.data) {
        return await setTcbCloudImages(res.data);
    }
    return [];
}

/**
 * 发送意见反馈信息
 * @returns {Promise<void>}
 */
export const addFeedBack = async (data) => {
    if (Utils.isNull(data.name)) {
        return alert('请输入您的姓名！');
    }
    if (Utils.isNull(data.email)) {
        return alert('请输入您的邮箱！')
    }
    if (!Utils.checkEmail(data.email)) {
        return alert('邮箱格式错误！')
    }
    if (Utils.isNull(data.phone)) {
        return alert('请输入您的电话！')
    }
    if (!Utils.checkPhone(data.phone)) {
        return alert('电话格式错误！')
    }
    if (Utils.isNull(data.message)) {
        return alert('请输入内容！')
    }

    let opt = {
        name: data['name'],
        email: data['email'],
        mobile: data['phone'],
        content: data['message'],
        _createTime: new Date()
    };
    return await db.collection('hc-feedback').add(opt);
}